/*
  学习目标：💥 默认Action不能有异步行为，React要求是Action只是一个普通的JS对象
 
*/

import {
  TODO_DEL_BY_ID,
  TODO_Update_By_Id,
  TODO_UPDATE_TYPE,
} from "./actionTypes";

export const delByIdAction = (payload) => ({
  type: TODO_DEL_BY_ID,
  payload,
});

export const updateByIdAction = (payload) => ({
  type: TODO_Update_By_Id,
  payload,
});

export const updateTypeAction = (payload) => ({
  type: TODO_UPDATE_TYPE,
  payload,
});

export const addAction = (tak) => {
  return async (dispatch) => {
    const newItem = await loadNewTodoAPI();

    dispatch({
      type: "todo/add",
      payload: newItem,
    });
  };
};

function loadNewTodoAPI() {
  const newTodo = {
    task: "异步返回的任务",
    id: Date.now(),
    isDone: false,
  };
  console.log("开始请求");
  return new Promise((resolve) => {
    setTimeout(() => {
      console.log("请求成功，2秒后,数据返回");
      resolve(newTodo);
    }, 2000);
  });
}
